//文本审核
const express = require('express');
const router = express.Router();
const axios = require('axios');
const Green20220302 = require('@alicloud/green20220302');
const OpenApi = require('@alicloud/openapi-client');
const Util = require('@alicloud/tea-util');
const Tea = require('@alicloud/tea-typescript');


/* GET home page. */
router.post('/', async (req, res, next) => {

    try {    
        const { userText } = req.body
        
        //校验参数是否合法
        if ( !userText) {
            res.status(400).json({
                code: 400,
                msg: '缺少必要的参数',
                data:''
            })
            return
        }

        //调用阿里云内容审核
        let config = new OpenApi.Config({
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
            accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
            accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
            
        });
        config.endpoint = `green-cip.cn-shanghai.aliyuncs.com`;

        let client = new Green20220302.default(config);

        let textModerationRequest = new Green20220302.TextModerationRequest({ 
                  service: 'comment_detection',
      serviceParameters:'{"content":"'+(userText)+'"}'
        });

        let runtime = new Util.RuntimeOptions({});

        try {
            let resp = await client.textModerationWithOptions(textModerationRequest, runtime);
            res.json({
                    result:resp.body.data
            });
        }
        catch (error) {
            res.json({
                code: 200,
                msg: '失败',
                data: {
                    result:error
                }
            });
        }

    }
    catch (e) {
        res.status(401).json({
            msg: 401,
            message: '访问异常' + e.message
        });
    }

});


function check(id) {
    
}


module.exports = router;
